import * as mutationTypes from './mutationTypes'
import { playMode } from 'common/js/config'
import { shuffle } from 'common/js/utils'
import {
  saveSearch,
  deleteSearch,
  clearSearch,
  savePlay,
  saveFavorite,
  deleteFavorite
} from 'common/js/cache'

function findIndex (list, song) {
  return list.findIndex((item) => {
    return item.rid === song.rid
  })
}

// 每个 action 都可以接受两个参数，
// 第一个参数为 context 对象，其中可以获取到 commit、state
// 第二个参数为调用该方法时接收的数据
export const selectPlay = ({ commit, state }, { list, index }) => {
  commit(mutationTypes.SET_SEQUENCE_LIST, list)

  if (state.mode === playMode.random) {
    const randomList = shuffle(list)
    commit(mutationTypes.SET_PLAYLIST, randomList)
    index = findIndex(randomList, list[index])
  } else {
    commit(mutationTypes.SET_PLAYLIST, list)
  }

  commit(mutationTypes.SET_CURRENT_INDEX, index)
  commit(mutationTypes.SET_FULL_SCREEN, true)
  commit(mutationTypes.SET_PLAYING_STATE, true)
}

// 随机播放
export const randomPlay = ({ commit, state }, { list }) => {
  commit(mutationTypes.SET_MODE_STATE, playMode.random)
  commit(mutationTypes.SET_SEQUENCE_LIST, list)

  const randomList = shuffle(list)
  commit(mutationTypes.SET_PLAYLIST, randomList)

  commit(mutationTypes.SET_CURRENT_INDEX, 0)
  commit(mutationTypes.SET_FULL_SCREEN, true)
  commit(mutationTypes.SET_PLAYING_STATE, true)
}

// 插入歌曲
export const insertSong = ({ commit, state }, song) => {
  let playlist = [...state.playlist]
  let sequenceList = [...state.sequenceList]
  let currentIndex = state.currentIndex

  // 记录当前歌曲
  let currentSong = playlist[currentIndex]

  // 查找当前播放列表中，是否已经有该待插入的歌曲，并返回其索引
  let fpIndex = findIndex(playlist, song)

  // 因为是插入歌曲，所以索引要加 1
  currentIndex++

  // 插入这首歌到当前索引位置(playlist)
  playlist.splice(currentIndex, 0, song)

  // 如果已经包含了这首歌，即 fpIndex > -1
  if (fpIndex > -1) {
    if (currentIndex > fpIndex) {
      // 如果当前插入的序号大于播放列表中的序号，则直接删除之前的那首歌
      playlist.splice(fpIndex, 1)
      // 由于删除了一个，因此数组长度减 1，currentIndex 也要减 1
      currentIndex--
    } else {
      // 如果当前插入的序号小于播放列表中的序号，则先加 1后，再删除之前的那首歌
      playlist.splice(fpIndex + 1, 1)
    }
  }

  // 向 sequenceList 中插入的位置(当前歌曲 currentSong 的后面)
  let currentSIndex = findIndex(sequenceList, currentSong) + 1

  // 查找 sequenceList 之前是否已经有这首歌曲
  let fsIndex = findIndex(sequenceList, song)

  // 插入这首歌到当前索引位置(sequenceList)
  sequenceList.splice(currentSIndex, 0, song)

  // 如果 sequenceList 中已经包含了这首歌，即 fsIndex > -1
  if (fsIndex > -1) {
    if (currentSIndex > fsIndex) {
      sequenceList.splice(fsIndex, 1)
      // 这里不需要修改 currentSIndex
    } else {
      sequenceList.splice(fsIndex + 1, 1)
    }
  }

  // 提交
  commit(mutationTypes.SET_PLAYLIST, playlist)
  commit(mutationTypes.SET_SEQUENCE_LIST, sequenceList)
  commit(mutationTypes.SET_CURRENT_INDEX, currentIndex)
  commit(mutationTypes.SET_FULL_SCREEN, true)
  commit(mutationTypes.SET_PLAYING_STATE, true)
}

// 保存搜索历史
export const saveSearchHistory = ({ commit }, query) => {
  commit(mutationTypes.SET_SEARCH_HISTORY, saveSearch(query))
}

// 删除搜索历史
export const deleteSearchHistory = ({ commit }, query) => {
  commit(mutationTypes.SET_SEARCH_HISTORY, deleteSearch(query))
}

// 清空所有搜索历史
export const clearSearchHistory = ({ commit }) => {
  commit(mutationTypes.SET_SEARCH_HISTORY, clearSearch())
}

// 删除一首歌
export const deleteSong = ({ commit, state }, song) => {
  let playlist = [...state.playlist]
  let sequenceList = [...state.sequenceList]
  let currentIndex = state.currentIndex

  // 调用 findIndex 方法，从 playlist 中找到 song 对应的 index
  let pIndex = findIndex(playlist, song)
  // 在 playlist 中删掉这首歌
  playlist.splice(pIndex, 1)

  // 调用 findIndex 方法，从 sequenceList 中找到 song 对应的 index
  let sIndex = findIndex(sequenceList, song)
  // 在 sequenceList 中删掉这首歌
  sequenceList.splice(sIndex, 1)

  // 当前播放歌曲索引(currentIndex)大于 pIndex 或者等于最后一个(playlist.length)
  // currentIndex 都需要减 1，因为 pIndex 对应歌曲被删除了
  if (currentIndex > pIndex || currentIndex === playlist.length) {
    currentIndex--
  }

  commit(mutationTypes.SET_PLAYLIST, playlist)
  commit(mutationTypes.SET_SEQUENCE_LIST, sequenceList)
  commit(mutationTypes.SET_CURRENT_INDEX, currentIndex)

  // 如果 playlist 为空，则让歌曲暂停播放
  // 否则，则让歌曲播放
  const playingState = playlist.length > 0
  commit(mutationTypes.SET_PLAYING_STATE, playingState)
}

// 清空播放列表
export const deleteSongList = ({ commit }) => {
  commit(mutationTypes.SET_PLAYLIST, [])
  commit(mutationTypes.SET_SEQUENCE_LIST, [])
  commit(mutationTypes.SET_CURRENT_INDEX, -1)
  commit(mutationTypes.SET_PLAYING_STATE, false)
}

// 保存播放历史
export const savePlayHistory = ({ commit }, song) => {
  commit(mutationTypes.SET_PLAY_HISTORY, savePlay(song))
}

// 保存收藏歌曲
export const saveFavoriteList = ({ commit }, song) => {
  commit(mutationTypes.SET_FAVORITE_LIST, saveFavorite(song))
}

// 删除收藏歌曲
export const deleteFavoriteList = ({ commit }, song) => {
  commit(mutationTypes.SET_FAVORITE_LIST, deleteFavorite(song))
}
